package abcy.cloud.syscoreserver.services.impls;

import abcy.cloud.blogcommon.enums.SignEnum;
import abcy.cloud.blogcommon.utils.UUIDUtil;
import abcy.cloud.syscoreserver.dto.req.SysWhiteAddReqDto;
import abcy.cloud.syscoreserver.dto.resp.SysWhiteSelectRespDto;
import abcy.cloud.syscoreserver.entities.SysWhite;
import abcy.cloud.syscoreserver.mappers.SysWhiteMapper;
import abcy.cloud.syscoreserver.services.SysWhiteService;
import abcy.cloud.syscoreserver.utils.CodeUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.security.Signature;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class SysWhiteServiceImpl implements SysWhiteService {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
    @Autowired
    private SysWhiteMapper sysWhiteMapper;
    @Override
    public boolean addWhite(SysWhiteAddReqDto sysWhiteAddReqDto) {
        SysWhite sysWhite = new SysWhite();
        BeanUtils.copyProperties(sysWhiteAddReqDto, sysWhite);
        Map<String, Object> map = (Map<String, Object>) redisTemplate.opsForValue().get("userInfo");
        String code = CodeUtil.codeGenerator(SignEnum.WHITE.getValue(), sysWhiteMapper.selectMaxCode());
        sysWhite.setCode(code);
        Date date = new Date();
        sysWhite.setCreateTime(date);
        sysWhite.setUpdateTime(date);
        sysWhite.setCreateBy(map.get("userId").toString());
        sysWhite.setUpdateBy(map.get("userId").toString());
        sysWhite.setIsDel(false);
        sysWhite.setId(UUIDUtil.UUID());
        Integer flag = sysWhiteMapper.insert(sysWhite);
        if (flag == 1) {
            return true;
        }
        return false;
    }

    @Override
    public List<String> selectWhiteList() {
        List<SysWhiteSelectRespDto> whiteList = sysWhiteMapper.selectWhiteList();
        List<String> stringList = whiteList.stream().map(e -> e.getUrl()).collect(Collectors.toList());
        return stringList;
    }
}
